package com.company.ch03;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Person implements Comparable<Person> {//如果使用sorted()排序，使用该行
    //public class Person {//如果使用sorted(Comparator<T>)排序,使用该行
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //省略setter、getter...

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    //以下是IDEA自动重写的equals()和hashCode()方法，就是根据当前类的属性name、age是否相同来判断是否是同一个对象
    @Override
    public boolean equals(Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         Person person = (Person) o;
         return age == person.age && Objects.equals(name, person.name);
    }
    @Override
    public int hashCode() {
         return Objects.hash(name, age);
    }

    @Override
    public String toString() {
         return "Person{"+
                  "name='"+ name + '\'' +
                  ", age="+ age +
                  '}';
    }

    @Override
    public int compareTo(Person p) {
         if(this.getAge() > p.getAge())return 1 ;
         else if(this.getAge() < p.getAge())return -1 ;
         else return 0;
    }

    public static void main(String[] args) {
        Person person = new Person("张三", 12);
        Person person1 = new Person("张三",6);
        Person person2 =new Person("张三",12);
        Person person3 =new Person("张三",18);

        System.out.println(person.compareTo(person1));
        System.out.println(person.compareTo(person2));
        System.out.println(person.compareTo(person3));
        List<Person> l = new ArrayList();

        l.add(person);
        l.add(person1);
        l.add(person2);
        l.add(person3);
//        System.out.println(l.sort(););

    }
}